package com.fh.controller;

import com.fh.common.ResultEnum;
import com.fh.common.ResultMessage;
import com.fh.pojo.CarDTO;
import com.fh.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    private CarService carService;


    @RequestMapping("queryCarList")
    public ResultMessage queryCarList(Long userId){
        try {
            List<CarDTO> carList = carService.queryCarListUserId(userId);
            return new ResultMessage(ResultEnum.QUERY_SUCCESS,carList);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(ResultEnum.QUERY_ERROR);
        }
    }

    @PostMapping("addCar")
    public ResultMessage addCar(Long userId, Long pid , Long count){
        try {
            List<CarDTO> carDTOlist = carService.addCar(userId, pid, count);
            return new ResultMessage(ResultEnum.INSERT_SUCCESS,carDTOlist);
        }catch (Exception e){
                e.printStackTrace();
            return new ResultMessage(ResultEnum.INSERT_ERROR);
        }

    }

    @RequestMapping("deleteCar.sjsp")
    public ResultMessage deleteCar(Long pid, Long userId){
        try {
            carService.deleteCar(pid,userId);
            return new ResultMessage(ResultEnum.INSERT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(ResultEnum.INSERT_ERROR);
        }
    }

}
